<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/20
 * Time: 9:20
 */
namespace app\index\controller;

use think\Controller;
use think\Db;

use app\index\controller\Base;

class UserInfo extends Base {




    private $bank_model = null;
    private $user_attribute = null;
    private $finance_mol = null;

    //初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->bank_model = new \app\common\model\user\Bank;
        $this->user_attribute = new \app\common\model\user\Attribute;
        $this->finance_mol = new \app\common\model\Finance;
    }


    //个人中心
    public function index(){
        $uid  = $this->uid;
        $_where["uid"] = $uid;
        $user_info = $this->user_model->where($_where)->find();
        $this->assign("info",$user_info);
        return $this->fetch('shop/user');
    }
    //提现
    public function withdraw(){
        $uid  = $this->uid;
        $_where["uid"] = $uid;
        $user_info = $this->bank_model->with(["user"=>function($q){$q->field("balance,uid,username");}])->where($_where)->find();
        $this->assign("info",$user_info);
        return $this->fetch('shop/withdraw');
    }
    //金豆明细
    public function beanDetail(){
        $uid  = $this->uid;
        $_where["uid"] = $uid;
        $_where["source"] = "paike";
        $list = $this->finance_mol->where($_where)->page(1,10)->field("desc,fina_type,fina_cash,on_time,balance,source")->group("fina_id")->order("on_time DESC")->select();
        unset($_where["source"]);
        $user_info = $this->user_model->where($_where)->field("balance")->find();
        //halt($list);
        $this->assign("list",$list);
        $this->assign("balance",$user_info["balance"]);
        return $this->fetch('shop/beanDetail');
    }
    //金豆明细刷新
    public function beanList(){
        $page      = input('page')?:1;//页数
        $page_size = input('page_size')?:10;//页码数
        $uid  = $this->uid;
        $_where["uid"] = $uid;
        $_where["source"] = "paike";
        $data["count"]  = ceil(($this->finance_mol->where($_where)->count())/$page_size);
        $data["curr"]   = $page;
        $list  = $this->finance_mol
            ->where($_where)
            ->page($page,$page_size)
            ->field("desc,fina_type,fina_cash,on_time,balance,source")
            ->group("fina_id")
            ->order("on_time DESC")
            ->select();
        foreach ($list as &$value){
            $value["on_time"] = date("Y-m-d H:i:s",$value["on_time"]);
        }
        $data["list"] = $list;
        renderJson("任务列表",1,$data);

    }
    //个人信息
    public function userInfo(){
        $uid  = $this->uid;
        $_where["uid"] = $uid;
        $user_info = $this->user_model->with(["userAttribute"=>function($q){$q->field("wx_code,uid,card_no,card_pic");}])->where($_where)->find();
        $this->assign("info",$user_info);
        return $this->fetch('shop/userInfo');
    }
    //修改密码
    public function changepsw(){
        return $this->fetch('shop/changepsw');
    }
    //添加银行卡
    public  function  addcard(){
        return $this->fetch('shop/addcard');
    }
    //我的组员
    public function members(){
        $uid  = $this->uid;
        $page = 1;
        $page_size = 20;
        $_join[] = ["sd_user_real l","l.uid = e.uid","left"];
        $_join[] = ["sd_user_attribute u","u.uid = e.uid","left"];
        $list =  Db::name("user_tree")
            ->alias("e")
            ->join($_join)
            ->where("e.parent_id",$uid)
            ->field("e.username,e.mobile,e.on_time,u.month_task_count,l.real_status")
            ->page($page,$page_size)
            ->order("e.on_time desc")
            ->select();
        $this->assign("list",$list);
        return $this->fetch('shop/members');
    }

    //ajax 组员渲染
    public function member_list(){
        $uid  = $this->uid;
        $page = input("page")?:1;
        $page_size = input("page_size")?:20;
        $index = input("type");
        $_where = [];
        if($index==1 || $index==3){
            $_where["e.parent_id"] = $uid;//待审核组员和我的组员用户父id当条件
            if($index==3) {
                $_where["l.real_status"] = 1;//待审核组员还需要添加一个条件real_status==1
            }
        }elseif($index==2 || $index==4){//待审核组员和我的组员用户父id当条件找第二级用户
            $parent_info = Db::name("user_tree")->where("uid",$uid)->find();
            $parent_left = $parent_info["lft"];
            $parent_rght = $parent_info["rgt"];
            $level = $parent_info["level"];
            $tree_id = $parent_info["tree_id"];
            $_where["e.lft"] = array(">=",$parent_left);
            $_where["e.rgt"] = array("<=",$parent_rght);
            $_where["e.level"]=$level+2;
            $_where["e.tree_id"] = $tree_id;
            if($index==4) {
                $_where["l.real_status"] = 1;
            }
        }
        $_join[] = ["sd_user_real l","l.uid = e.uid","left"];
        $_join[] = ["sd_user_attribute u","u.uid = e.uid","left"];
        $data["count"] = ceil((Db::name("user_tree")->alias("e")->join($_join)->where($_where)->count())/$page_size);
        $data["index"] = $index;
        $data["list"] =  Db::name("user_tree")
            ->alias("e")
            ->join($_join)
            ->where($_where)
            ->field("e.username,e.mobile,FROM_UNIXTIME(e.on_time) as on_time,u.month_task_count,l.real_status,e.level")
            ->page($page,$page_size)
            ->order("e.on_time desc")
            ->select();
         renderJson("成功执行",1,$data);
    }


    //待审核组员
    public function noverify(){
        $uid  = $this->uid;
        $page = 1;
        $page_size = 20;
        $_join[] = ["sd_user_real l","l.uid = e.uid","left"];
        $_join[] = ["sd_user_attribute u","u.uid = e.uid","left"];
        $_where["l.real_status"] = "1";
        $list =  Db::name("user_tree")
            ->alias("e")
            ->join($_join)
            ->where("e.parent_id",$uid)
            ->where($_where)
            ->field("e.username,e.mobile,e.on_time,u.month_task_count,l.real_status")
            ->page($page,$page_size)
            ->order("e.on_time desc")
            ->select();
        $this->assign("list",$list);
        return $this->fetch('shop/noverify');
    }


    //邀请注册流程
    public function guide(){
        return $this->fetch('shop/guide');
    }

    //接单教程
    public function tutorial (){
        return $this->fetch('shop/tutorial');
    }
















}